package com.gmzg.gmzgnews.news.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.gmzg.gmzgnews.R;
import com.gmzg.gmzgnews.live.adapter.PicturePageAdapter;
import com.gmzg.gmzgnews.news.WebViewActivity;
import com.gmzg.gmzgnews.news.adapters.NewsHeadAdapter;
import com.gmzg.gmzgnews.news.entries.HeadNews;
import com.gmzg.gmzgnews.news.entries.HeadTop;
import com.gmzg.gmzgnews.news.enums.NewsType;
import com.gmzg.gmzgnews.news.interfaces.NeteaseServer;
import com.jash.networklibrary.NetworkTask;
import com.jash.networklibrary.Tools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class HeadFragment extends BaseFragment implements NetworkTask.Callback<HeadTop>, SwipeRefreshLayout
        .OnRefreshListener, NewsHeadAdapter.OnChildClickListener {
    private static final String TAG = "HeadFrament";
    private ViewPager mNewsHeadViewpager;
    @BindView(R.id.news_head_recyclerView)
    RecyclerView mNewsHeadRecyclerView;
    @BindView(R.id.news_head_refreshLayout)
    SwipeRefreshLayout mNewsHeadRefreshLayout;
    private List<HeadNews> mHeadNewsList;
    private NewsHeadAdapter mNewsHeadAdapter;
    private int rows = 50;
    private NeteaseServer mNeteaseServer;

    //RecycleView是否正在刷新
    public HeadFragment() {
        // Required empty public constructor
    }

    @Override
    public String getFragmentTitle() {
        return "头条";
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_news_head, container, false);
        ButterKnife.bind(this, ret);
        mHeadNewsList = new ArrayList<>();
        initRecyclerView();
        mNewsHeadRefreshLayout.setOnRefreshListener(this);
        return ret;
    }

    private void initGetData() {
        mNeteaseServer = Tools.getInstance(NeteaseServer.class);
        mNeteaseServer.getHeadNews(1,10,1).execute(this);
    }

  /*  *//**
     * 网络数据的获取
     *//*
    private void initGetData() {
        HeadGetData headGetData = RetrofitHelper.getHeadDataApi();
        mHeadNewsList.clear();
        Toast.makeText(getContext(), "数据获取，清空List", Toast.LENGTH_SHORT).show();
        Call<HeadTop> call = headGetData.getList("news", 10, rows, 1);
        call.enqueue(this);
    }*/

    private void initRecyclerView() {
        mNewsHeadAdapter = new NewsHeadAdapter(getContext(), mHeadNewsList, NewsType.TYPE_HEAD);
        mNewsHeadRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        mNewsHeadRecyclerView.setAdapter(mNewsHeadAdapter);
        initGetData();
//        initViewPager();
        mNewsHeadAdapter.setOnChildClickListener(this);
    }

    public void initViewPager() {
//        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.news_head_viewpage, null);
//        mNewsHeadViewpager = (ViewPager) inflate.findViewById(R.id.news_head_viewpager);
        //添加广告轮播
        ViewPager headView= (ViewPager)LayoutInflater.from(getContext()).inflate(R.layout.live_hot_ad,null);
        List<Integer> images = new ArrayList<>();
        images.add(R.drawable.live_hot_ad1);
        images.add(R.drawable.live_hot_ad2);
        images.add(R.drawable.live_hot_ad3);
        images.add(R.drawable.live_hot_ad4);
        PicturePageAdapter pageAdapter = new PicturePageAdapter(getContext(), images);
        headView.setAdapter(pageAdapter);
        mNewsHeadAdapter.addHeadView(headView);
        /*List<BaseFragment> fragmentList = new ArrayList<>();
        fragmentList.add(new News_ImageView_First_Fragment());
        fragmentList.add(new News_ImageView_Second_Fragment());
        fragmentList.add(new News_ImageView_Third_Fragment());
        fragmentList.add(new News_ImageView_Fouth_Fragment());
        CommonFragmentPagerAdapter adapter = new CommonFragmentPagerAdapter
                (getChildFragmentManager(), fragmentList);*/
//        mNewsHeadAdapter.addHeadView(inflate);
//        mNewsHeadViewpager.setAdapter(adapter);
    }

   /* @Override
    public void onResponse(Call<HeadTop> call, Response<HeadTop> response) {
        mHeadNewsList.addAll(response.body().getList());
        //setRefreshing(false)表示图标消失
        mNewsHeadRefreshLayout.setRefreshing(false);
        Log.d(TAG, "onResponse: "+ mHeadNewsList.get(0).getMessage());
        mNewsHeadAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<HeadTop> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), "网络请求错误", Toast.LENGTH_SHORT).show();
    }*/

    @Override
    public void onRefresh() {
        initGetData();
    }

    @Override
    public void onChildClick(RecyclerView parent, View view, int position, HeadNews data) {
        Toast.makeText(getContext(), data.getTitle()+"被点击了", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(getContext(), WebViewActivity.class);
        intent.putExtra("998", mHeadNewsList.get(position).getMessage());
        startActivity(intent);
    }

    @Override
    public void onSuccess(HeadTop text) {
        mNewsHeadAdapter.addAll(text.getList());
        mNewsHeadRefreshLayout.setRefreshing(false);
        mNewsHeadAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(Exception e) {
            e.printStackTrace();
    }
}
